<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件上传</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#3B82F6',
                        secondary: '#64748B'
                    },
                    borderRadius: {
                        'none': '0px',
                        'sm': '2px',
                        DEFAULT: '4px',
                        'md': '8px',
                        'lg': '12px',
                        'xl': '16px',
                        '2xl': '20px',
                        '3xl': '24px',
                        'full': '9999px',
                        'button': '4px'
                    }
                }
            }
        }
    </script>
    <style>
        .drag-active {
            border: 2px dashed #3B82F6 !important;
            background: rgba(59, 130, 246, 0.05);
        }
        .progress-bar {
            transition: width 0.3s ease;
        }
        .toast {
            animation: slideIn 0.3s ease, fadeOut 0.3s ease 2.7s forwards;
        }
        @keyframes slideIn {
            from { transform: translateY(-100%); opacity: 0; }
            to { transform: translateY(0); opacity: 1; }
        }
        @keyframes fadeOut {
            from { opacity: 1; }
            to { opacity: 0; }
        }
    </style>
</head>
<body class="min-h-[1024px] bg-gray-50">
    <div class="max-w-[1440px] mx-auto px-8 py-6">
        <header class="flex justify-between items-center mb-8">
            <h1 class="text-2xl font-semibold text-gray-800">文件上传</h1>
            <button class="flex items-center px-4 py-2 text-secondary hover:text-primary transition-colors !rounded-button">
                <i class="fas fa-question-circle mr-2"></i>
                <span>帮助</span>
            </button>
        </header>

        <div class="bg-white rounded-lg p-8 shadow-sm mb-8">
            <div id="upload-zone" class="border-2 border-dashed border-gray-300 rounded-lg p-12 text-center cursor-pointer hover:border-primary transition-colors">
                <div class="flex flex-col items-center">
                    <i class="fas fa-cloud-upload-alt text-6xl text-gray-400 mb-4"></i>
                    <p class="text-gray-600 mb-2">拖拽文件到此处或点击上传</p>
                    <p class="text-gray-400 text-sm">支持格式：PNG、JPG、PDF、WORD、EXCEL</p>
                </div>
                <input type="file" id="file-input" class="hidden" multiple>
            </div>
        </div>

        <div class="bg-white rounded-lg p-8 shadow-sm mb-8" id="list">

        </div>

        <div class="flex justify-between items-center">
            <div class="text-gray-500">
                已上传：<span class="text-primary">2</span> / 3
            </div>
            <div class="space-x-4">
                <button class="px-6 py-2 bg-gray-200 text-gray-700 hover:bg-gray-300 transition-colors !rounded-button whitespace-nowrap">
                    取消上传
                </button>
                <button class="px-6 py-2 bg-primary text-white hover:bg-blue-600 transition-colors !rounded-button whitespace-nowrap">
                    开始上传
                </button>
            </div>
        </div>
    </div>

    <div id="toast" class="fixed top-4 right-4 bg-green-500 text-white px-6 py-3 rounded-lg shadow-lg hidden toast">
        文件上传成功
    </div>

    <script>
        const uploadZone = document.getElementById('upload-zone');
        const fileInput = document.getElementById('file-input');

        uploadZone.addEventListener('click', () => {
            fileInput.click();
        });

        ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
            uploadZone.addEventListener(eventName, preventDefaults, false);
            document.body.addEventListener(eventName, preventDefaults, false);
        });

        function preventDefaults(e) {
            e.preventDefault();
            e.stopPropagation();
        }

        ['dragenter', 'dragover'].forEach(eventName => {
            uploadZone.addEventListener(eventName, highlight, false);
        });

        ['dragleave', 'drop'].forEach(eventName => {
            uploadZone.addEventListener(eventName, unhighlight, false);
        });

        function highlight() {
            uploadZone.classList.add('drag-active');
        }

        function unhighlight() {
            uploadZone.classList.remove('drag-active');
        }

        uploadZone.addEventListener('drop', handleDrop, false);

        function handleDrop(e) {
            const dt = e.dataTransfer;
            const files = dt.files;
            handleFiles(files);
        }

        function handleFiles(files) {

            let uploadImg = files[0];
            console.log(uploadImg);
            let formData = new FormData();
            formData.append("img", uploadImg);

            fetch("http://localhost:3000/upload", {
                method: "POST",
                body: formData
            }).then(res => res.json()).then(result => {
                console.log(result);
            })

            // const toast = document.getElementById('toast');
            // toast.classList.remove('hidden');
            // setTimeout(() => {
            //     toast.classList.add('hidden');
            // }, 3000);
        }

        fileInput.addEventListener('change', (e) => {
            handleFiles(e.target.files);
        });

        window.onload = function() {
            fetch("http://localhost:3000/list")
                .then(res => res.json())
                .then(result => {
                    let list = document.getElementById("list");
                    for(let i = 0; i < result.length; i++) {
                        let imgSrc = result[i];
                        let imgNode = document.createElement("img");
                        imgNode.src = imgSrc;
                        imgNode.style.height = "200px";
                        imgNode.style.width = "200px";
                        list.appendChild(imgNode);
                    }
                })
        }
    </script>
</body>
</html>

